$(document).ready(function(){
	//定义接口
	var add_category_url = "/blog/category/save/";
	var list_category_url = "/blog/category/list/";
	var add_link_url = "/blog/link/save/";
	var list_link_url = "/blog/link/list/";
	var add_diary_url = "/blog/diary/save/";
	var list_diary_url = "/blog/diary/list/";
	
	//设定ajax全局参数
	$("div#ajax_message").ajaxStart(function(){
		$(this).css("visibility", "visible");
	}); 
	$("div#ajax_message").ajaxSuccess(function(){   
		$(this).css("visibility", "hidden");
	});
	
	//设置fckeditor全局按参数
	$.fck.config = {
		path: '/media/fckeditor/',
		height: 360
	};
	
	//对象CURD
	function delete_category(){
		id = $(this).attr("id").replace("delete_category_", "");
		$.ajax({
			type: "POST",
			url: "/blog/category/" + id + "/delete/",
			success: function(html){
				$("#category_list").html(html);
				$("div[id*='delete_category_']").click(delete_category);
			}
		});
	}
	
	function load_category_list(){
		$.ajax({
			type: "GET",
			url: list_category_url,
			success: function(html){
				$("#category_list").html(html);
				$("div[id*='delete_category_']").click(delete_category);
			}
		});
	}
	
	function delete_link(){
	}
	
	function load_link_list(){
		$.ajax({
			type:"GET",
			url: list_link_url,
			success:function(html){
				$("#link_list").html(html);
				$("div[id*='delete_link_']").click(delete_link);
			}
		});
	}
	
	function delete_diary(){
		id = $(this).attr("id").replace("delete_diary_", "");
		$.ajax({
			type: "POST",
			url: "/blog/diary/" + id + "/delete/",
			success: function(html){
				$("#diary_list").html(html);
				$("div[id*='delete_diary_']").click(delete_diary);
			}
		});
	}
	
	function load_diary_list(){
		$.ajax({
			type:"GET",
			url: list_diary_url,
			success:function(html){
				$("#diary_list").html(html);
				$("div[id*='delete_diary_']").click(delete_diary);
			}
		});
	}
	
	function add_comment(){
		id = $(this).attr("id").replace("add_comment_", "");
		$.ajax({
			type: "GET",
			url: "/blog/" + id + "/comment/save/",
			async: false,
			success: function(html){
				add_comment_dlg.html(html);
				add_comment_dlg.dialog("open");
			}
		});
	}
	
	//声明dialog
	var add_category_dlg = $("#add_category_dialog");
	add_category_dlg.dialog({
		modal:true,
		autoOpen: false,
		title:"添加日志类别",
		height:200,
		buttons: {
			'取消': function() {
				$(this).dialog('close');
				$(this).empty();
			},
			'保存': function() {
				var data = $("#add_category_form").ajaxForm().formToArray();
				$.post(add_category_url, data);
				load_category_list();
				$(this).dialog('close');
				$(this).empty();
			}
		}
	});
	
	var add_link_dlg = $("#add_link_dialog");
	add_link_dlg.dialog({
		modal:true,
		autoOpen: false,
		title:"添加链接",
		height:260,
		buttons: {
			'取消': function() {
				$(this).dialog('close');
				$(this).empty();
			},
			'保存': function() {
				var data = $("#add_link_form").ajaxForm().formToArray();
				$.post(add_link_url, data);
				load_link_list();
				$(this).dialog('close');
				$(this).empty();
			}
		}
	});
	
	var add_diary_dlg = $("#add_diary_dialog");
	add_diary_dlg.dialog({
		modal:true,
		autoOpen: false,
		title:"发表日志",
		height:540,
		width:1048,
		buttons: {
			'取消': function() {
				$(this).dialog('close');
				$(this).empty();
			},
			'保存': function() {
				var id = '';
				var data = $("#add_diary_form").ajaxForm().formToArray();
				for (var i=0; i< data.length; i++){   
			        if (data[i].name == "diary_content"){   
			        	data[i].value = $.fck.content("id_diary_content", "");   
			        }
			        if (data[i].name == "diary_key"){
			        	id = data[i].value;
			        }
			    } 
				if (id != ''){
					add_diary_url = "/blog/diary/" + id + "/save/"
				}
				$.post(add_diary_url, data);
				load_diary_list();
				load_category_list();
				$(this).dialog('close');
				$(this).empty();
			}
		}
	});
	
	var add_comment_dlg = $("#add_comment_dialog");
	add_comment_dlg.dialog({
		modal:true,
		autoOpen: false,
		title:"我也评论",
		height:430,
		width:465,
		buttons: {
			'取消': function() {
				$(this).dialog('close');
				$(this).empty();
			},
			'保存': function() {
				var form_data = $("#add_comment_form").ajaxForm().formToArray();
				for (var i=0; i< form_data.length; i++){   
			        if (form_data[i].name == "diary_id"){   
			        	id = form_data[i].value;   
			        }
			    }
				$.ajax({
					type: "POST",
					url: "/blog/" + id + "/comment/save/",
					data: form_data,
					async: false,
					success: function(html){
						$('#diary_list').html(html);
						$("input[id*='add_comment_']").click(add_comment);
					}
				});
				$(this).dialog('close');
				$(this).empty();
			}
		}
	});
	
	//添加点击事件处理
	$("div#add_category").click(function(){
		$.ajax({
			type: "GET",
			url: add_category_url,
			async: false,
			success: function(html){
				add_category_dlg.html(html);
				add_category_dlg.dialog("open");
			}
		});
	});

	$("div#add_link").click(function(){
		$.ajax({
			type: "GET",
			url: add_link_url,
			async: false,
			success: function(html){
				add_link_dlg.html(html);
				add_link_dlg.dialog("open");
			}
		});
	});
	
	$("div#add_diary").click(function(){
		$.ajax({
			type: "GET",
			url: add_diary_url,
			async: false,
			success: function(html){
				add_diary_dlg.html(html);
				$('textarea#id_diary_content').fck();
				add_diary_dlg.dialog("open");
			}
		});
	});
	
	$("div[id*='edit_diary_']").click(function(){
		id = $(this).attr("id").replace("edit_diary_", "");
		$.ajax({
			type: "GET",
			url: "/blog/diary/" + id + "/save/",
			async: false,
			success: function(html){
				add_diary_dlg.html(html);
				$('textarea#id_diary_content').fck();
				add_diary_dlg.dialog("open");
			}
		});
	});
	
	$("input[id*='add_comment_']").click(add_comment);
	
	$("a[href*='/blog/diary/']").click(function(){
		$.ajax({
			type: "GET",
			url: $(this).attr('href'),
			async: false,
			success: function(html){
				$('#diary_list').html(html);
				$("input[id*='add_comment_']").click(add_comment);
			}
		});
		return false;
	});
	
	$("a[href*='/blog/category/']").click(function(){
		$.ajax({
			type: "GET",
			url: $(this).attr('href'),
			async: false,
			success: function(html){
				$('#diary_list').html(html);
			}
		});
		return false;
	});
	
	$("a[href*='/blog/tag/']").click(function(){
		$.ajax({
			type: "GET",
			url: $(this).attr('href'),
			async: false,
			success: function(html){
				$('#diary_list').html(html);
			}
		});
		return false;
	});
	
	//删除点击事件处理
	$("div[id*='delete_diary_']").click(delete_diary);
	$("div[id*='delete_link_']").click(delete_link);
	$("div[id*='delete_category_']").click(delete_category);
});
